﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.BaoCao
{
    public partial class XemTinhTrang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCatalog.BindPhongHoc(cboPhongHoc);
                Pager1.CurrentIndex = 1;
                BindHoatDong(Pager1.CurrentIndex);
            }
        }

        protected void cboPhongHoc_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            H_PhongHocId.Value = cboPhongHoc.SelectedValue;
            BindHoatDong(Pager1.CurrentIndex);
        }

        private void BindHoatDong(int PageNo)
        {
            int totalRow = 0;
            string whereClause = "";
            switch (H_PhongHocId.Value)
            {
                case "0":
                    whereClause += "MaThietBi in (Select MaThietBi from ThietBi)";
                    break;
                default:
                    whereClause += "MaThietBi in (Select MaThietBi from ThietBi where (MaPhong= " + H_PhongHocId.Value + "))";
                    break;
            }
            if (chkTrangThai.Checked == true)
            {
                whereClause += " and MaThietBi in (Select MaThietBi from ThietBi where TrangThai=0)";
            }
            whereClause += " and MaCanBo=" + Common.GetKeyUserInfo("Id");
            DataSet dsHoatDong = DataRepository.Provider.ExecuteDataSet("HoatDongSuaChua_GetPaged", whereClause, "NgayCapNhat DESC",
                PageNo - 1, Pager1.PageSize);
            rpHoatDong.DataSource = dsHoatDong.Tables[0];
            rpHoatDong.DataBind();
            try
            {
                totalRow = Convert.ToInt32(dsHoatDong.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = totalRow;
            if (totalRow <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            BindHoatDong(currnetPageIndex);
        }

        public string getTenThietBi(object ob)
        {
            string id = ob.ToString();
            return DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id)).TenThietBi;

        }

        public string getLoaiTinhTrang(object ob)
        {
            string id = ob.ToString();
            return DataRepository.LoaiTinhTrangProvider.GetByMaLoaiTinhTrang(Convert.ToInt32(id)).TenLoaiTinhTrang;

        }

        public string getTenPhong(object ob)
        {
            string id = ob.ToString();
            return DataRepository.PhongHocProvider.GetByMaPhong(DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id)).MaPhong).TenPhong;

        }

        public string getTinhTrang(object ob)
        {
            string id = ob.ToString();
            ThietBi ent_ThietBi = DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id));
            if (ent_ThietBi.TrangThai == true)
                return "<html><img src=../../Images/checkk.png /></html>";
            else
            {
                return "<html><img src=../../Images/redx.png /></html>";
            }

        }

        protected void chkTrangThai_OnCheckedChanged(object sender, EventArgs e)
        {
            BindHoatDong(Pager1.CurrentIndex);
        }

        protected void cmdPrint_OnClick(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(uppanel1, this.GetType(), "onclick", "PrintDiv()", true);
        }

        public string getHuongGiaiQuyen(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            ChiTietHoatDongSuaChua ent_ChiTiet = DataRepository.ChiTietHoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(id);
            if (ent_ChiTiet != null)
            {
                return ent_ChiTiet.HuongGiaiQuyet;
            }
            else
            {
                return "Chờ cập nhật";
            }
        }
        public string getNgaySuaChua(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            ChiTietHoatDongSuaChua ent_ChiTiet = DataRepository.ChiTietHoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(id);
            if (ent_ChiTiet != null)
            {
                if (ent_ChiTiet.NgaySuaChua.ToString() == "NULL" || ent_ChiTiet.NgaySuaChua.ToString() == "")
                {
                    return "Đang giải quyết";
                }
                else
                {
                    return Convert.ToDateTime(ent_ChiTiet.NgaySuaChua).ToString("dd/MM/yyyy");
                }
            }
            else
            {
                return "Đang giải quyết";
            }
        }
    }
}